package com.mercator.client.util;

import org.vaadin.gwtgraphics.client.Shape;

import com.allen_sauer.gwt.log.client.ConsoleLogger;
import com.allen_sauer.gwt.log.client.Log;
import com.allen_sauer.gwt.log.shared.LogRecord;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.dev.json.JsonObject;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.json.client.JSONException;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.jsonp.client.JsonpRequestBuilder;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.mercator.client.graph.DContainer;
import com.mercator.client.graph.DGeneSetMap;
import com.mercator.client.graph.GenStringNode;
import com.mercator.client.graph.GenType;
import com.mercator.client.graph.Gene;
import com.mercator.client.graph.GeneSet;
import com.mercator.client.util.JSONRequest.JSONRequestHandler;

public class JSOParser {
	private static final String MATRIX =
	// GS - dictionary mapping the GeneSet IDs to associated ontology terms.
	// MATRIX - dictionary that maps Gene IDs to all GeneSet IDs in the project
	// (true/false).
	// INFO - dictionary of parameters and results from the tool execution.
	// TODO: cross-site scripting
	// private static final String MATRIX =
	// "http://ontologicaldiscovery.org/~jjay/avner/?runhash=9f03fd663c275a991b47e23f6cd16326&t=matrix";
	// Hosted mode local fix:
	// "http://127.0.0.1:8888/data";
	// private static final String GS = "http://127.0.0.1:8080/data2";

	// App local temp fix:
	// "http://odemercator.appspot.com/data";
	// private static final String GS = "http://odemercator.appspot.com/data2";
	// compiled
	// private static final String MATRIX =
	// "file:///home/avnermaiberg/development/mercator/ode-mercator/war/data";
	// "http://umcs.maine.edu/~maiber81/data";
	// "http://192.168.1.147:8080/data.json";
	"http://172.16.247.5:8080/data.json";

	// "http://130.111.197.15:8080/data.json";
	// full?
	// "war/data";

	// TODO: Parse Ontologies
	/*
	 * public boolean generateGraph(final DGeneSetMap g){ String url = MATRIX;
	 * RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);
	 * 
	 * try { try { Request request = builder.sendRequest(url, new
	 * RequestCallback() { public void onError(Request request, Throwable
	 * exception) { System.err.println("Couldn't retrieve JSON");
	 * Window.alert("failed to parse JSON"); }
	 * 
	 * public void onResponseReceived(Request request, Response response) { if
	 * (200 == response.getStatusCode()) { JSONValue jsonValue =
	 * JSONParser.parseLenient(response.getText()); JSONObject jsonObject =
	 * jsonValue.isObject(); if(jsonObject !=null) { Window.alert("parsing" );
	 * for (String s : jsonObject.keySet()) { Gene gene = new Gene(s);
	 * g.add(gene); JSONObject genes = jsonObject.get(s).isObject(); //
	 * Window.alert(gene + ""); if (genes != null) for (String k :
	 * genes.keySet()) { // System.out.println(genes.get(k)); if
	 * (Boolean.parseBoolean(genes.get(k).toString())) { GeneSet geneset = new
	 * GeneSet(k); g.add(geneset); g.connect(geneset, gene); }
	 * 
	 * }
	 * 
	 * } } }}}); } catch (RequestException e) {
	 * Window.alert("failed to parse JSON"); e.printStackTrace(); } } finally{
	 * 
	 * }; return false; } }
	 */

	public boolean generateGraph(final DGeneSetMap g) {
		String url = MATRIX;
		// url = URL.encode(url);

		// final JsonpRequestBuilder builder = new JsonpRequestBuilder();
		JSONRequest.get(url, new JSONRequestHandler() {

			@Override
			public void onRequestComplete(JavaScriptObject json) {
				JSONGeneArray response = JSONGeneArray.create(json);
				if (response != null) {
					Window.alert("parsing");
					for (String s : JSONGeneArray.iterator(response)) {
						Gene gene = new Gene(s);
						g.add(gene);
						JSONGene genes = response.get(s);
						// Window.alert(gene + "");
						if (genes != null)
							for (String k : genes.iterator()) {
								// System.out.println(genes.get(k));
								if (genes.get(k)) {
									GeneSet geneset = new GeneSet(k);
									g.add(geneset);
									g.connect(geneset, gene);
								}
							}
					}
				}

			}

		});
		return false;
	}
}
